<?php
namespace app\common\model;
use think\Request;

/**
 * 评论模型类
 */
class Comment extends BaseModel
{
    protected $name = 'comment';
	
    /**
     * 关联用户
	*/
    public function user()
    {
        return $this->hasOne('User','user_id','user_id');
    }
	
	/**
     * 关联产品
    */
    public function goods()
    {
        return $this->hasOne('Goods','goods_id','goods_id');
    }
	
	/**
     * 评论图片
     */
    public function getImgAttr($value)
    {
		if($value){
			return ['value' => 1, 'url' => './uploads/'.$value ,'text' => $value];
		}
		return ['value' => 0]; //没有图片
    }
	
	/**
     * 显示状态
     */
    public function getIsShowAttr($value)
    {
		$status = [0 => '隐藏', 1 => '显示'];
        return ['text' => $status[$value], 'value' => $value];
    }
	
	/**
     * 获取列表
	 * $is_show,为空=获取所有，0=隐藏的，1=显示中
    */
    public function getList($shop_id=0,$is_show='')
    {
		 // 筛选条件
        $filter = [];
        !empty($is_show) && $filter['is_show'] = $is_show;
		$shop_id>0 && $filter['shop_id'] = $shop_id;
        $list = $this->with(['user','goods'])
            ->where($filter)
            ->order(['comment_id' => 'desc','comment_id' => 'asc'])
            ->paginate(15, false, ['query' => Request::instance()->request()]);
        return $list;
    }
	
	/**
     * 详情
     */
    public static function detail($comment_id)
    {
        return self::get($comment_id,['user','goods']);
    }

    /**
     * 根据条件统计数量
     */
    public static function getCount($shop_id=0)
    {
        $self = new static;
		 // 筛选条件
        $filter = [];
        $shop_id > 0 && $filter['shop_id'] = $shop_id;
		$wxapp_id = $self::$wxapp_id;
		$wxapp_id > 0 && $filter['wxapp_id'] = $wxapp_id;
        $count = array();
        //全部
        $count[0] = self::useGlobalScope(false)->where($filter)->count();
        //今天
        $star = strtotime(date("Y-m-d"),time());
        $count[1] = self::useGlobalScope(false)->where('create_time','>',$star)->where($filter)->count();
        //昨天
        $star = strtotime("-1 day");
        $end = strtotime(date("Y-m-d"),time());
        $count[2] = self::useGlobalScope(false)->where('create_time','>',$star)->where('create_time','<',$end)->where($filter)->count();
        //前天
        $star = strtotime("-2 day");
        $end = strtotime("-1 day");
        $count[3] = self::useGlobalScope(false)->where('create_time','>',$star)->where('create_time','<',$end)->where($filter)->count();
        //-4天
        $star = strtotime("-3 day");
        $end = strtotime("-2 day");
        $count[4] = self::useGlobalScope(false)->where('create_time','>',$star)->where('create_time','<',$end)->where($filter)->count();
        //-5天
        $star = strtotime("-4 day");
        $end = strtotime("-3 day");
        $count[5] = self::useGlobalScope(false)->where('create_time','>',$star)->where('create_time','<',$end)->where($filter)->count();
        //-6天
        $star = strtotime("-5 day");
        $end = strtotime("-4 day");
        $count[6] = self::useGlobalScope(false)->where('create_time','>',$star)->where('create_time','<',$end)->where($filter)->count();
        //-7天
        $star = strtotime("-6 day");
        $end = strtotime("-5 day");
        $count[7] = self::useGlobalScope(false)->where('create_time','>',$star)->where('create_time','<',$end)->where($filter)->count();
        return $count;
    }

}
